package com.develop.common;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
 
public class JDBCUtil {
 
    public static String DRIVERNAME = null;
    public static String URL = null;
    public static String USER = null;
    public static String PASSWORD = null;
 
    public static Connection conn = null;
 
    	
    public static Connection getConnection() throws Exception {
    	Properties props = new Properties();
    	//Reader in = new FileReader("db.properties");
    	InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("application.properties");
    	props.load(in);
    	
    	DRIVERNAME = props.getProperty("drivername");
    	URL = props.getProperty("url");
    	USER = props.getProperty("user");
    	PASSWORD = props.getProperty("password");
        if (conn != null) {
            return conn;
        }
        Class.forName(DRIVERNAME);
        conn = DriverManager.getConnection(URL, USER, PASSWORD);
 
        return conn;
    }
 
    public static void closeResource(Connection conn, PreparedStatement st) throws SQLException {
        st.close();
        conn.close();
    }
 
    public static void closeResource(Connection conn, ResultSet rs, PreparedStatement st) throws SQLException {
        st.close();
        rs.close();
        conn.close();
    }
}